package com.winhxd.b2c.common.util;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类
 * @author mahongliang
 * @date  2018年8月18日 下午6:55:38
 * @Description 
 * @version
 */
public class DateUtil {
	private final  static String  deafult_format="yyyy-MM-dd HH:mm:ss";
	
	/**
	 * 时间转字符串
	 * @author mahongliang
	 * @date  2018年8月18日 下午6:55:51
	 * @Description 
	 * @param date
	 * @param format
	 * @return
	 */
	public static String format(Date date, String format){
		if(date == null){
			date = new Date();
		}
		if(StringUtils.isBlank(format)) {
			return  null;
		}
		return new SimpleDateFormat(format).format(date);
	}
	/**
	* @Description: date转String
	* @Author: liuhanning
	* @Date: 2019/1/16 15:47
	*/
	public static String dateToString(Date date, String format){
		if(date == null){
			return  "";
		}
		if(StringUtils.isBlank(format)) {
			format=deafult_format;
		}
		return new SimpleDateFormat(format).format(date);
	}

	public static String dateToString(Date date){
		return dateToString(date,deafult_format);
	}
	/**
	* @Description: string转date
	* @Author: liuhanning
	* @Date: 2019/1/16 15:49
	*/
	public static Date stringToDate(String dateStr, String format)  {
		if(StringUtils.isBlank(dateStr)){
			return  null;
		}
		if(StringUtils.isBlank(format)) {
			format=deafult_format;
		}
		Date date= null;
		try {
			date = new SimpleDateFormat(format).parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date ;
	}

	public static Date stringToDate(String date){
		return stringToDate(date,deafult_format);
	}

	/**
	 * 时间字符串转日期
	 * @author mahongliang
	 * @date  2018年8月18日 下午8:05:13
	 * @Description 
	 * @param strDate
	 * @param format
	 * @return
	 */
	public static Date toDate(String strDate, String format) throws ParseException {
		if(StringUtils.isEmpty(format)) {
			return null;
		}

		return new SimpleDateFormat(format).parse(strDate);
	}

	public static Date getStartDate(Date startDate){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(startDate);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);

		return calendar.getTime();
	}

	public static Date getEndDate(Date endDate){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(endDate);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 59);

		return calendar.getTime();
	}

	public static Date getEndDate(Date endDate,Integer effectiveDays){

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(endDate);
		calendar.add(Calendar.DATE, effectiveDays);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 59);

		return calendar.getTime();
	}

	public static Date getDateAddMinute(Date date,int minute){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MINUTE, minute);
		return calendar.getTime();
	}

	public static void main(String[] args) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String ss = format.format(getDateAddMinute(new Date(),120));
		System.out.println("==="+(System.currentTimeMillis() > getDateAddMinute(new Date(),120).getTime()));
		System.out.print(dateToString(new Date(),"yyyy年MM月dd HH:mm:ss"));
	}

}
